
-----------------------------------------------------------------------
-- MatrixDebug.lua
--
-- A Framework for World Of Warcraft addons
-- Copyright (C) 2007  wwkkii
-- 
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-- 
-- Home Page: http://code.google.com/p/wowmatrix
-----------------------------------------------------------------------

local __version__ = { "MatrixDebug", 0, 1, 1 };
if (MatrixUpdateVersion(__version__)) then return; end

-----------------------------------------------------------------------
-- variables
-----------------------------------------------------------------------

local output_level = 50;
local debug_levels = { };

local function Matrix_GenerateException(idx)
	error("MATRIX_ERROR", idx + 3);
end

function MatrixDebug(name, mesg, ...)
	local level;

	level = debug_levels[name];
	if (not level) then
		debug_levels[name] = 0;
		level = 0;
	end
	if (level < output_level) then
		return;
	end
	mesg = matrix.TextInfo(mesg, ...);
	matrix.Print(mesg);
end

matrix.EnableDebug = function(level)
	if (not level) then
		level = 0;
	elseif (level > 100) then
		level = 100;
	end
	output_level = level;
end

matrix.GetDebugLevel = function(name)
	local level;

	if (not name) then
		return output_level;
	end

	level = debug_levels[name];
	if (not level) then
		return 0;
	end
	return leve;
end

matrix.SetDebugLevel = function(name, level)
	if (not level) then
		level = 0;
	elseif (level >= 100) then
		level = 99;
	end
	if (name == "[ALL]") then
		for name in pairs(debug_levels) do
			debug_levels[name] = level;
		end
	else
		debug_levels[name] = level;
	end
end

matrix.DumpStack = function()
	local idx, result, mesg, file, num, info;

	for idx = 1, 100, 1 do
		result, mesg = pcall(Matrix_GenerateException, idx);
		file, num, info = string.match(mesg, "(.-):(%d+):(.*)");
		if (file and num and info) then
			matrix.Print(idx..": "..file.."("..num..")");
		end
	end
end
